/**
 * Created by Liutong on 12/24/15.
 * 商品调整
 */
(function(window,document){
    $(document).ready(function(){
        $('#availableModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget) // Button that triggered the modal
            var id = button.data('id') // Extract info from data-* attributes
            var modal = $(this);
            modal.find('#available-ok').attr('data-id', id);
        });

        $("#available-ok").click(function(){
            $("#able").attr("style","display: none");
            var id = $(this).data("id");
            $.ajax({
                type: "GET",
                url: getContextPath()+"/products/updateAvailable?id="+id,
                success: function (data) {
                    if(data.available==0){
                        $("#success").removeAttr("style");
                    }
                    if(data.available==1){
                        $("#failed").removeAttr("style");
                    }
                }
            });
        });

        $("#close").click(function(){
            window.location.href = getContextPath()+"/products/products";
        });
        $("#failed-close").click(function(){
            window.location.href = getContextPath()+"/products/products";
        });


        $('#categorys').cxSelect({
            url: getContextPath()+'/services/private/category/all',
            selects: ['category-lv1', 'category-lv2', 'category-lv3'],
            nodata: 'none'
        });

        $('#datetimepicker_start').datetimepicker({
            language:"zh"
            //defaultDate: moment().format("YYYY-MM-DD 00")
        });
        $('#datetimepicker_end').datetimepicker({
            language:"zh"
            //defaultDate: moment().format("YYYY-MM-DD HH")
        });

        $("#datetimepicker_start").on("dp.change",function (e) {
            $('#datetimepicker_end').data("DateTimePicker").setMinDate(e.date);
        });
        $("#datetimepicker_end").on("dp.change",function (e) {
            $('#datetimepicker_start').data("DateTimePicker").setMaxDate(e.date);
        });

        $('#unavailableModal').modal('hide').css({
            'margin-top': '100px'
        });

        $('#productId').numeric();
        $('#tbIid').numeric();
        $('#storeId').numeric();
        $('#brandId').numeric();
        $("#startPrice").numeric();
        $("#endPrice").numeric();


        $("#btn_search").bind("click",function(){
            $("#currentPage").val("1");

            if($("#productId").val() != "" && !isDigit($("#productId").val())){
                alert("商品ID必须为数字");
                return false;
            }

            if($("#tbIid").val() != "" && !isDigit($("#tbIid").val())){
                alert("淘宝IID必须为数字");
                return false;
            }

            if($("#storeId").val() != "" && !isDigit($("#storeId").val())){
                alert("渠道ID必须为数字");
                return false;
            }

            if($("#brandId").val() != "" && !isDigit($("#brandId").val())){
                alert("品牌ID必须为数字");
                return false;
            }

            if($("#startPrice").val() != "" && !isDigit($("#startPrice").val())){
                alert("价格区间必须为数字");
                return false;
            }

            if($("#endPrice").val() != "" && !isDigit($("#endPrice").val())){
                alert("价格区间必须为数字");
                return false;
            }

            $("#myForm").submit();
        });

        //----hover 到小图上显示大图效果
        var hideImageTimer;
        $(".product-img").on("mouseover",function(event){
            //清除timer
            if(hideImageTimer){
                clearTimeout(hideImageTimer);
            }

            var imgY = event.clientY-20;
            var clientHight = document.documentElement.clientHeight;
            console.log(event.clientWidth);
            if(imgY+300 > clientHight){
                imgY = clientHight - 300;
            }
            $("#hoverImage").attr("src",$(this).attr("src"));
            if($("#hoverImage").is(":hidden")){
                $("#hoverImage").css("left",event.clientX+50);
                $("#hoverImage").css("top",imgY);
                $("#hoverImage").fadeIn();
            } else {
                $("#hoverImage").animate({"top":imgY+"px"});
            }
        });

        $(".product-img").on("mouseout",function(event){
            hideImageTimer = setTimeout(function(){
                $("#hoverImage").fadeOut();
            },300);
        });
        //---hover end

        function isDigit(s)
        {
            var patrn=/^[0-9]{1,20}$/;
            if (!patrn.exec(s)) return false;
            return true;
        }

    });
})(window,document);